package qq.android.wan.qq.chat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.Toast;

import com.easemob.chat.EMCallStateChangeListener;
import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EMServiceNotReadyException;

import qq.android.wan.qq.R;

/**
 * Created by Administrator on 2016/1/10.
 */
public class CallActivity extends Activity {
    private GridView gv_operate;
    private boolean isComingCall;//进入这个界面时的标志，为true说明是别人打进来的，为false时说明是你要给别人打电话了

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_chat_call);
        initUI();
        initListener();
        Intent intent = getIntent();
        String from = getIntent().getStringExtra("from");//跟你通话的对方的ID
        isComingCall = intent.getBooleanExtra("isComingCall", false);//默认是你主动给别人打电话
        try {
            //向from发起通话请求
            EMChatManager.getInstance().makeVoiceCall(from);
        } catch (EMServiceNotReadyException e) {
            e.printStackTrace();
        }

    }

    private void initListener() {
        //监听连接状态,该监听是在子线程，不能修改UI，若修改UI就调用runOnUiThread
        EMChatManager.getInstance().addVoiceCallStateChangeListener(new EMCallStateChangeListener() {
                                                                        @Override
                                                                        public void onCallStateChanged(EMCallStateChangeListener.CallState callState, CallError error) {
                                                                            Log.e("test", "错误:" + error);
                                                                            final CallError mError = error;
                                                                            switch (callState) {
                                                                                case CONNECTING: // 正在连接对方
                                                                                    Log.e("test", "正在连接对方");
                                                                                    //Toast.makeText(CallActivity.this,"正在连接对方",Toast.LENGTH_SHORT).show();
                                                                                    break;
                                                                                case CONNECTED: // 双方已经建立连接
                                                                                    Log.e("test", "双方已经建立连接");
                                                                                    // Toast.makeText(CallActivity.this,"双方已经建立连接",Toast.LENGTH_SHORT).show();
                                                                                    break;

                                                                                case ACCEPTED: // 电话接通成功
                                                                                    Log.e("test", "电话接通成功");
                                                                                    // Toast.makeText(CallActivity.this,"电话接通成功",Toast.LENGTH_SHORT).show();
                                                                                    break;
                                                                                case DISCONNNECTED: // 电话断了
                                                                                    Log.e("test", "电话断了");

                                                                                    runOnUiThread(new Runnable() {
                                                                                        @Override
                                                                                        public void run() {
                                                                                            if (mError == CallError.ERROR_INAVAILABLE) {
                                                                                                //不在线
                                                                                                Log.e("test", "不在线");
                                                                                            } else if (mError == CallError.ERROR_BUSY) {
                                                                                                //对方正在通话中，请稍后再拨
                                                                                                Log.e("test", "对方正在通话中，请稍后再拨");
                                                                                            } else if (mError == CallError.ERROR_TRANSPORT) {
                                                                                                //建立连接失败
                                                                                                Log.e("test", "建立连接失败");
                                                                                            } else if (mError == CallError.ERROR_NORESPONSE) {
                                                                                                //对方未接听
                                                                                                Log.e("test", "对方未接听");
                                                                                            } else if (mError == CallError.REJECTED) {
                                                                                                //对方拒绝接受，不是挂断
                                                                                                Log.e("test", "对方拒绝接受，不是挂断");
                                                                                            }
                                                                                        }
                                                                                    });

                                                                                    // Toast.makeText(CallActivity.this,"电话断了",Toast.LENGTH_SHORT).show();
                                                                                    break;
                                                                                case NETWORK_UNSTABLE: //网络不稳定
                                                                                    if (error == CallError.ERROR_NO_DATA) {
                                                                                        //无通话数据
                                                                                        Log.e("test", "网络不稳定");
                                                                                        //  Toast.makeText(CallActivity.this,"网络不稳定",Toast.LENGTH_SHORT).show();
                                                                                    } else {
                                                                                    }
                                                                                    break;
                                                                                case NETWORK_NORMAL: //网络恢复正常
                                                                                    //Toast.makeText(CallActivity.this,"",Toast.LENGTH_SHORT).show();
                                                                                    break;
                                                                                default:
                                                                                    break;
                                                                            }

                                                                        }
                                                                    }

        );
    }

    private void initUI() {
        gv_operate = (GridView) findViewById(R.id.gv_operate);
    }

    class GridAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            return null;
        }
    }
}
